package com.googlecode.marrowboy.assertionresult;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Encapsulates a single test class, (possibly) containing multiple test
 * methods.
 * 
 * @author mjg
 */
public class ClassDetail {

	private final String name;
	private final Map<String, MethodDetail> methodResults;

	/**
	 * Constructor.
	 * 
	 * @param className
	 *            the name of the class
	 */
	public ClassDetail(String className) {
		methodResults = new LinkedHashMap<String, MethodDetail>();
		this.name = className;
	}

	/**
	 * Getter
	 * 
	 * @return the class name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Get a {@link MethodDetail} by name
	 * 
	 * @param methodName
	 *            the name of the method
	 * @return the MethodResult object for the given name. Will create a new
	 *         {@link MethodDetail} rather than return null.
	 */
	public MethodDetail getMethodResult(String methodName) {

		if (methodResults.get(methodName) == null) {
			methodResults.put(methodName, new MethodDetail(methodName));
		}

		return methodResults.get(methodName);
	}

	/**
	 * How many methods passed in this class?
	 * 
	 * @return the number of methods which passed
	 */
	public int getPassCount() {
		int count = 0;
		for (MethodDetail methodResult : methodResults.values()) {
			if (methodResult.allPassed()) {
				count++;
			}
		}
		return count;
	}

	/**
	 * How many methods failed in this class?
	 * 
	 * @return the number of assertions which methods
	 */
	public int getFailCount() {
		int count = 0;
		for (MethodDetail methodResult : methodResults.values()) {
			if (!methodResult.allPassed()) {
				count++;
			}
		}
		return count;
	}

	/**
	 * How many methods in this class?
	 * 
	 * @return the number of methods
	 */
	public int getTotalCount() {
		return methodResults.size();
	}

	/**
	 * Gets all the {@link MethodDetail}s in this {@link ClassDetail}.
	 * 
	 * @return a <code>Collection&lt;MethodResult&gt;</code> of all method
	 *         results in this class
	 */
	public Collection<MethodDetail> getMethodResults() {
		return methodResults.values();
	}

	/**
	 * Convenience method to check if all methods passed
	 * 
	 * @return whether all the methods passed
	 */
	public boolean allPassed() {
		return (getFailCount() == 0);
	}

}
